package com.bmps.springcloud.insuredmicroservice.service.trial.strategy;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.trail.TrialPersonModel;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.trial.TrialUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author : zlc-（Jacob）
 * @date : 2019/10/29 16:12
 **/
public class Aat070TrialStrategy implements TrialStrategy{
    
    private static Logger logger = LoggerFactory.getLogger(Aat070TrialStrategy.class);

    @Override
    public TrialPersonModel premiumTrialCalculationPrepare(ProductModel productModel, RequestModel requestModel, RedisService redisService) {
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        for(InsurantModel insurantModel :insurantModelList) {
            if (insurantModel.getInsSeq() == productModel.getProdInsSeq()) {
                TrialPersonModel trialPersonModel = new TrialPersonModel();
                String riskCode = productModel.getProdCode();
                trialPersonModel.setRiskCode(riskCode);
                
                trialPersonModel.setGet(productModel.getAmnt());
                trialPersonModel.setInsuYear(productModel.getInsureYear());
                trialPersonModel.setInsuYearFlag(productModel.getInsureYearFlag());
                //   设置年龄
                trialPersonModel.setAppAge(DateUtil.getYears(insurantModel.getBirthday(),  requestModel.getPolicyInfo().getPolApplyDate()));
                trialPersonModel.setPayEndYear(productModel.getPayYear());
                trialPersonModel.setInsuYearFlag(productModel.getPayYearFlag());
                
                String payIntv = requestModel.getProductInfo().getPayIntv();
                switch (payIntv){
                    case "1": payIntv = "12"; break;
                    case "3": payIntv = "12"; break;
                    case "6": payIntv = "12"; break;
                    case "12":payIntv = "12"; break;  
                    default: payIntv = "0";   
                }
                // 设置交费频率
                trialPersonModel.setPayIntv(payIntv);
                
                String payIntvRate = requestModel.getProductInfo().getPayIntv();
                trialPersonModel.setPayIntvRate("1");
                // 设置打折系数
                if (StringUtils.equals("1", payIntvRate)) {
                    trialPersonModel.setPayIntvRate("0.09");
                }
                if (StringUtils.equals("3", payIntvRate)) {
                    trialPersonModel.setPayIntvRate("0.262");
                }
                if (StringUtils.equals("6", payIntvRate)) {
                    trialPersonModel.setPayIntvRate("0.52");
                }

                StringBuilder sb = new StringBuilder(40);

                sb.append(AcceptConstant.REDIS_PREFIX_R004).append("|").append(riskCode);
                Object object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                TrialUtil.setSpecialElement(trialPersonModel, object.toString());
                logger.info("【" + productModel.getProdCode() + "】【VPU】【KEY】" + sb.toString());

                // 获取要素顺序
                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R001).append("|").append(riskCode);
                // 拼接要素顺序的KEY
                logger.info("【"+riskCode+"】【产品计算要素配置】【KEY】"+sb.toString());
                object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                // object.toString() 缓存要素顺序 必须要全部获取到值
                logger.info("【要素顺序】" + object.toString());
                // 根据要素顺序拼接费率KEY
                String result = TrialUtil.getRiskRateKey(trialPersonModel,object.toString());
                logger.info("【费率KEY】" +result);
                object = redisService.get(result);
                if (object == null) {
                    logger.info("【费率结果】" +object);
                    return new TrialPersonModel();
                }
                TrialUtil.setElementRate(trialPersonModel, object.toString());

                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R005).append("|").append(riskCode).append("|G");
                logger.info("【"+riskCode+"】【产品计算公式要素配置】【KEY】"+sb.toString());
                object = redisService.get(sb.toString());
                if (object == null) {
                    logger.info("【"+riskCode+"】【产品计算公式要素配置结果】"+object);
                    return new TrialPersonModel();
                }
                logger.info("【"+riskCode+"】【产品计算公式要素配置结果】"+object);
                // 元素对应关系
                TrialUtil.setElementMapping(trialPersonModel, object.toString());

                // 获取公式
                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R006).append("|")
                        .append(trialPersonModel.getRiskCode()).append("|G");
                logger.info("【" + riskCode + "】【公式】【KEY】" + sb.toString());
                //设置险种对应的计算公式
                object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                trialPersonModel.setFormula(object.toString());

                return trialPersonModel;
            }
        }
        return new TrialPersonModel();
    }
}
